package com.wifipositioner.measure;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

import android.net.wifi.ScanResult;

public class MeasuredPoint implements Serializable{
	
	private String name;
	
	public HashMap<String, AverageHelper> scanMap;
	
	public MeasuredPoint() {
	}
	
	public MeasuredPoint(String name, List<ScanResult> scanresult) {
		this(name);
		for(ScanResult result : scanresult){
			this.scanMap.put(result.SSID, new AverageHelper(result.level));
		}
	}
	
	public MeasuredPoint(String name) {
		this.name = name;
		this.scanMap = new HashMap<String, AverageHelper>();
	}

	public String getName() {
		return name;
	}

	public HashMap<String, AverageHelper> getScanMap() {
		return scanMap;
	}

	public void add(List<ScanResult> result) {
		for(ScanResult res : result){
			String SSID = res.SSID;
			int level = res.level;
			
			if(scanMap.containsKey(SSID)){
				scanMap.get(SSID).addVal(level);
			}
			else{
				scanMap.put(SSID, new AverageHelper(level));
			}
		}
		
	}

	/**
	 * 
	 * @return Array [dominantSSID, secondaryDominantSS] in the refpoint
	 */
	public String[] getDominantSSIDs() {
		double dominantSS = -Double.MAX_VALUE;
		double secondaryDominantSS = -Double.MAX_VALUE;
		String dominantSSID = "";
		String secondaryDominantSSID = "";
		for(String SSID : scanMap.keySet()){
			double level = scanMap.get(SSID).getAvg();
			if(level > dominantSS){
				secondaryDominantSS = dominantSS;
				secondaryDominantSSID = dominantSSID;
				dominantSS = level;
				dominantSSID = SSID;
			}
			else if(level > secondaryDominantSS){
				secondaryDominantSS = level;
				secondaryDominantSSID = SSID;
				
			}
		}
		return new String[]{dominantSSID, secondaryDominantSSID};
	}
}
